<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnbd.php");
	$MAIN = new MAIN();
}
$MAIN->users->checkLogin(true);
if(isset($_FILES['upload'])) {	
	if($MAIN->files->setFile($_FILES['upload'],$_POST)){
		?>
		<script type="text/javascript">document.location.href = 'index.php?cat=mydocuments&view=all';</script>
		<?php }else{
			echo("error! please contact administrator");
			}
			exit();
			}
			if(isset($_REQUEST['del'])){
			if($MAIN->files->delFile($_REQUEST['del'])){
	?>
	<script type="text/javascript">sendInfo(null, 'Filen har tagits bort!');
		(function() {
			document.location.href = 'index.php?cat=mydocuments&view=all';
		}).delay(3000);</script>
	<?php };
			exit();
			}
	?>
<div class="activ">
<?php
switch(@$_REQUEST['view']){	
	case 'add':	
?>
<h2>Ladda upp</h2>
<form name="frm_upload" id="frm_upload" action="mydocuments.php" method="post" enctype="multipart/form-data">
	<input type="hidden" name="txt_id" value="<?php echo($MAIN -> currentUser['id']); ?>"/>
	<table cellpadding="0" cellspacing="0">
		<!--<tr>
			<td><label for="txt_name">Namn :</label></td>
			<td><input type="text" name="txt_name" id="txt_name" value=""/></td>
		</tr>-->
		<tr>
			<td><label for="upload">Välj fil...</label></td>
			<td><input type="file" name="upload" id="upload"/></td>
		</tr>
		<tr>
			<td><label for="txt_name">Mapp :</label></td>
			<td>
				<select name="ddl_category" id="ddl_category">
				<?php
				$maps = $MAIN->categories->getCategories();
				foreach($maps as $map){
				?>
				<option value="<?php echo($map['id']); ?>"><?php echo($map['display_name']); ?></option>
				<?php } ?>
				</select>
			</td>
		</tr>
		<tr>
			<td><label for="txt_name">Beskrivning :</label></td>
			<td><textarea name="txt_desc" id="txt_desc"></textarea></td>
		</tr>
		<tr>
			<td></td>
			<td><input style="margin-left:120px;" type="submit" name="Submit" value="Submit"/></td>
		</tr>
	</table>
</form>
<?php
break;
default:
case 'all':
$ord="ASC";
$orderBy="created_on";
if(isset($_REQUEST['ordby'])){$orderBy=$_REQUEST['ordby'];}
$nextOrd="DESC";
if(isset($_REQUEST['ord'])){$ord=$_REQUEST['ord'];$nextOrd =($_REQUEST['ord']=="ASC")?"DESC":"ASC";}
if(isset($_REQUEST['s'])){
$files=$MAIN->files->searchMyFiles($_REQUEST['s']);
}else{
$files=$MAIN->files->getMyFiles($orderBy,$ord);
}
?>
<h2>Mina filer</h2>
<ul>
	<?php
	$parents = $MAIN->categories->getUserParentCategories();
	if($MAIN->currentUser['grouplevel']==0){
		$parents = $MAIN->categories->getParentCategories();
	}
	foreach($parents as $parent){
		$subCats =$MAIN->categories->getCategoriesByParent($parent['id']);
		$total = $MAIN->categories->countFilesUserCategory($parent['id']);
		?>
		<li>
			<?php
			if($total>0){
			?>
			<a href="javascript:void(0);" onclick="$('t_<?php echo($parent['id']); ?>').slide('toggle');"><?php echo($parent['name']); ?> [<?php echo($total)?> dokument]</a>
			<?php }else{ ?>
			<?php echo($parent['name']); ?> [<?php echo($total)?> dokument]
			<?php } ?>
			<?php
			if($MAIN->currentUser['grouplevel']<90){
			?>
			<a style="color:#78A73F;" href="index.php?cat=documents&amp;view=add&amp;fol=<?php echo($parent['id']); ?>">[ladda up]</a>
			<?php
			}
			?>
		<div class="documents" id="t_<?php echo($parent['id']); ?>">
			<table cellpadding="0" cellspacing="0" class="list" style="background-color:#e2e2e2; font-size:10px;">
				<tr>
					<th><a href="?cat=documents&amp;view=all&amp;ordby=name&amp;ord=<?php echo($nextOrd); ?>">Filnamn</a></th>
					<th style="width:10px;"></th>
					<th><a href="?cat=documents&amp;view=all&amp;ordby=created_on&amp;ord=<?php echo($nextOrd); ?>">Datum</a></th>
					<th style="width:10px;"></th>
					<th><a href="#">Filstorlek</a></th>
					<th style="width:10px;"></th>
					<th><a href="#">Ägare</a></th>
					<th style="width:10px;"></th>
					<th>Administrera</th>
				</tr>
				<?php
				foreach($files as $file){
					if($file['cat']==$parent['id']){
					$file['filename']=explode("/",$file['filename']);
					$file['filename']=$file['filename'][1]; 
				?>
				<tr class="entry">
					<td><a target="_blank" href="files/<?php echo($file['filename']); ?>" alt="<?php echo($file['desc']); ?>" title="<?php echo($file['desc']); ?>"><?php echo($file['name']); ?></a></td>
					<td></td>
					<td style="width:110px"><?php echo(date('Y-m-d H:s', strtotime($file['created_on']))); ?></td>
					<td></td>
					<td><?php echo(ceil($file['size'] / 1024)); ?> kb</td>
					<td></td>
					<td><?php echo($file['owner']); ?></td>
					<td></td>
					<td class="tools">
						<?php
						if($MAIN->currentUser['id']==$file['created_by'] || $MAIN->currentUser['grouplevel']<=10){
						?>
							<a href="javascript:void(0);" onclick="delFile(<?php echo($file['id']); ?>)"><img src="images/btn_admin_del.png" alt="Ta Bort" title="Ta Bort"/></a>
						<?php } ?>
					</td>
				</tr>
				<?php }
				} ?>
				</table>
			</div>
		<?php
		if(count($subCats)>0){
			?>
			<ul>
			<?php
			foreach($subCats as $category){
				$total2 = $MAIN->categories->countFilesUserCategory($category['id']);
				?>
				<li>
					<?php
					if($total2>0){
					?>
					<a href="javascript:void(0);" onclick="$('t_<?php echo($category['id']); ?>').slide('toggle');"><?php echo($category['name']); ?> [<?php echo($total2)?> dokumenter]</a>
					<?php }else{ ?>
					 <?php echo($category['name']); ?> [<?php echo($total2)?> dokument]
					<?php } ?>
					<?php
					if($MAIN->currentUser['grouplevel']<90){
					?>
					<a style="color:#78A73F;" href="index.php?cat=documents&amp;view=add&amp;fol=<?php echo($category['id']); ?>">[ladda up]</a>
					<?php
					}
					?>
				<div class="documents" id="t_<?php echo($category['id']); ?>">
					<table cellpadding="0" cellspacing="0" class="list" style="background-color:#e2e2e2; font-size:10px;">
						<tr>
							<th><a href="?cat=documents&amp;view=all&amp;ordby=name&amp;ord=<?php echo($nextOrd); ?>">Filnamn</a></th>
							<th style="width:10px;"></th>
							<th><a href="?cat=documents&amp;view=all&amp;ordby=created_on&amp;ord=<?php echo($nextOrd); ?>">Datum</a></th>
							<th style="width:10px;"></th>
							<th><a href="#">Filstorlek</a></th>
							<th style="width:10px;"></th>
							<th><a href="#">Ägare</a></th>
							<th style="width:10px;"></th>
							<th>Administrera</th>
						</tr>
						<?php
						foreach($files as $file){
							if($file['cat']==$category['id']){
							$file['filename']=explode("/",$file['filename']);
							$file['filename']=$file['filename'][1];
						?>
						<tr class="entry">
							<td><a target="_blank" href="files/<?php echo($file['filename']); ?>" alt="<?php echo($file['desc']); ?>" title="<?php echo($file['desc']); ?>"><?php echo($file['name']); ?></a></td>
							<td></td>
							<td style="width:110px"><?php echo(date('Y-m-d H:s', strtotime($file['created_on']))); ?></td>
							<td></td>
							<td><?php echo(ceil($file['size'] / 1024)); ?> kb</td>
							<td></td>
							<td><?php echo($file['owner']); ?></td>
							<td></td>
							<td class="tools">
								<?php
								if($MAIN->currentUser['id']==$file['created_by'] || $MAIN->currentUser['grouplevel']<=10){
								?>
									<a href="javascript:void(0);" onclick="delFile(<?php echo($file['id']); ?>)"><img src="images/btn_admin_del.png" alt="Ta Bort" title="Ta Bort"/></a>
								<?php } ?>
							</td>
						</tr>
						<?php }	
	} ?>
	</table>
				</div>
				</li>
				<?php } ?>
			</ul>
			<?php } ?>
		</li>
		<?php } ?>
</ul>
<script type="text/javascript">
	var delFile = function(id) {
		var msg = 'Bekräfta borttagning';
		if (confirm(msg)) {
			ajaxGet('mydocuments.php', 'del=' + id);
		}
	}
	$$('.documents').slide('hide');
</script>
<?php
break;
} ?>
</div>